# create grid 
library("ncdf4")
library("raster")
library("sf")
library("tidyverse")

conf.cellsize <- c(0.1, 0.1)

sst <- stack("input/cmems_mod_blk_phy-tem_my_2.5km_P1M-m_1715770301759.nc")
proj4string(sst)=CRS("+init=EPSG:4326")

map <- st_as_sf(as.data.frame(sst[[1]], xy=T), coords = c("x", "y"), crs="+init=EPSG:4326")
grid <- st_make_grid(map, cellsize=c(0.1,0.1), what="centers") #, what="centers"
# g <- st_as_sf(grid)

# from sf multi-point grid to dataframe
flatcoords <- do.call(rbind, st_geometry(grid)) %>% 
  as_tibble() %>% setNames(c("x","y"))

# save output 
write.csv(flatcoords, "output/grid.csv", row.names = F)
